package com.atguigu.webadmin.controller.apartment;


import com.atguigu.common.result.Result;
import com.atguigu.model.pojo.FeeKey;
import com.atguigu.model.pojo.FeeValue;
import com.atguigu.webadmin.service.FeeKeyService;
import com.atguigu.webadmin.service.FeeValueService;
import com.atguigu.webadmin.vo.fee.FeeKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "房间杂费管理")
@RestController
@RequestMapping("/admin/fee")
@CrossOrigin
public class FeeController {

    @Autowired
    private FeeKeyService feeKeyService;

    @Autowired
    private FeeValueService feeValueService;

    @Operation(summary = "保存或更新杂费名称")
    @Parameters({
            @Parameter(name = "feeKey",description = "杂费名称对象",in = ParameterIn.DEFAULT)
    })
    @PostMapping("key/saveOrUpdate")
    public Result<Boolean> saveOrUpdateFeeKey(@RequestBody FeeKey feeKey) {
        boolean b = feeKeyService.saveOrUpdate(feeKey);
        return Result.ok(b);
    }

    @Operation(summary = "保存或更新杂费值")
    @Parameters({
            @Parameter(name = "feeValue",description = "杂费值",in = ParameterIn.DEFAULT)
    })
    @PostMapping("value/saveOrUpdate")
    public Result<Boolean> saveOrUpdateFeeValue(@RequestBody FeeValue feeValue) {
        boolean b = feeValueService.saveOrUpdate(feeValue);
        return Result.ok(b);
    }


    @Operation(summary = "查询全部杂费名称和杂费值列表")
    @GetMapping("list")
    public Result<List<FeeKeyVo>> feeInfoList() {
        List<FeeKeyVo> feeKeyVoList = feeKeyService.feeInfoList();
        return Result.ok(feeKeyVoList);
    }

    @Operation(summary = "根据id删除杂费名称")
    @Parameters({
            @Parameter(name = "feeKeyId",description = "杂费id",in = ParameterIn.QUERY)
    })
    @DeleteMapping("key/deleteById")
    public Result<Boolean> deleteFeeKeyById(@RequestParam Long feeKeyId) {
        //先根据id逻辑删除名称
        feeKeyService.removeById(feeKeyId);
        //根据杂费名称对应的杂费值
        //创建Lambda查询包装对象
        LambdaQueryWrapper<FeeValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //eq.指定查询条件
        lambdaQueryWrapper.eq(feeKeyId != null,FeeValue::getFeeKeyId,feeKeyId);
        //根据指定的条件执行逻辑删除
        boolean b = feeValueService.remove(lambdaQueryWrapper);
        return Result.ok(b);
    }

    @Operation(summary = "根据id删除杂费值")
    @Parameters({
            @Parameter(name = "id",description = "杂费值id",in = ParameterIn.DEFAULT)
    })
    @DeleteMapping("value/deleteById")
    public Result<Boolean> deleteFeeValueById(@RequestParam Long id) {
        //根据杂费值id执行逻辑删除
        boolean b = feeValueService.removeById(id);
        return Result.ok(b);
    }
}
